package com.cn.yunhe.rentcars.web;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.mapper.CarMapper;
import com.cn.yunhe.rentcars.mapper.MaintainMapper;
import com.cn.yunhe.rentcars.model.Car;
import com.cn.yunhe.rentcars.model.Insurance;
import com.cn.yunhe.rentcars.model.Maintain;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@RestController
public class MaintainController {
	
	//增加	URL：supkeep/add method:post
	//删除	URL：supkeep/delete method:delete
	//查询	URL：supkeep/select method:get
	//修改	URL：supkeep/query method:put
	@Autowired
	private MaintainMapper maintainMapper;
	@Autowired
	private CarMapper carMapper ;
/*	@RequestMapping(value="/cspage", method=RequestMethod.GET)
	public ModelAndView cspage() {	
		return new ModelAndView("cs");
	}*/
	
	@RequestMapping(value="/maintain-list", method=RequestMethod.GET)
	public ModelAndView maintainList() {	
		
		return new ModelAndView("maintain-list");
		 
	}

	@RequestMapping(value="/maintain-add", method=RequestMethod.GET)
	public ModelAndView showAddMaintain() {	
		return new ModelAndView("maintain-add");
	}
	
	@RequestMapping(value="/maintain-edit", method=RequestMethod.GET)
	public ModelAndView showEditMaintain(@RequestParam String recordid) {	
		
		Maintain maintain = maintainMapper.getMaintainById(recordid);
		
		Map<String,Maintain> modelMap = new HashMap<String,Maintain>();
		modelMap.put("maintain", maintain);
		
		return new ModelAndView("maintain-edit",modelMap);		
	}
	
	@RequestMapping(value="/getmaintainbycarid", method=RequestMethod.GET)
	public PageInfo<Maintain> getMaintainbycarid(@RequestParam String carid,@RequestParam  int pageNum,@RequestParam  int pageSize){
	      PageHelper.startPage(pageNum, pageSize);
	      List<Maintain> maintainList = maintainMapper.getMaintain(carid);
	      PageInfo<Maintain> page = new PageInfo<Maintain>(maintainList);
	      return page;
	}
	
	
	/**
	 * 添加保养记录
	 * 内联修改车辆状态
	 * @param userid
	 * @param isabled
	 * 
	 * 
	 */
	@RequestMapping(value="/addMaintain", method=RequestMethod.POST)
	public void addMaintain(@RequestBody Maintain maintain){
		
		maintain.setRecordid(UUID.randomUUID().toString());
		maintainMapper.addMaintain(maintain);
		
		Car car = new Car ();
		if(maintain.getReturntime() != null){
			car.setCarstate(6);
			car.setMaintainmile(maintain.getNextmile());
			car.setMaintaintime(maintain.getNexttime());
		}else{
			car.setCarstate(9);
			
		}			
		car.setCarcode(maintain.getCarid());			
		carMapper.setCarAllstate(car);		
		
	}
	
	/**
	 * 修改保养记录
	 * 内联车辆状态修改
	 * @param recordid
	 */
	@RequestMapping(value="/maintain/{recordid}", method=RequestMethod.PUT)
	public void editMaintain(@PathVariable("recordid") String recordid,@RequestBody Maintain maintain){
		
		maintain.setRecordid(recordid);
		maintainMapper.editMaintain(maintain);

		if(maintain.getReturntime() != null){
			Car car = new Car ();
			car.setCarstate(6);
			car.setMaintainmile(maintain.getNextmile());
			car.setMaintaintime(maintain.getNexttime());
			car.setCarcode(maintain.getCarid());
			carMapper.setCarAllstate(car);
		}

	}
	
	/**
	 * 删除用户信息
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/delMaintain", method=RequestMethod.POST)
	public void delMaintain(@RequestParam String recordid){ 
		maintainMapper.delMaintain(recordid);
	}
	
	/**
	 * 删除用户信息
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/delMiantains", method=RequestMethod.POST)
	public void delMaintains(@RequestParam String recordid){ 
		
		List<String> recordidList = Arrays.asList(recordid.split(","));		
		maintainMapper.delMaintains(recordidList);
	}
	/**
	 * 搜索查询用户列表信息
	 * 
	 * @param username
	 * @param draw
	 * @param start
	 * @param length
	 * @return
	 */
	@RequestMapping(value="/maintain", method=RequestMethod.GET)
	public Map<String,Object> getLikeMaintain(@RequestParam String datemin,@RequestParam String datemax,@RequestParam String carid,@RequestParam int draw,@RequestParam int start,@RequestParam int length){
		  Map<String,String> params =  new HashMap<String,String>();
		  params.put("carid", carid);
		  params.put("datemin", datemin);
		  params.put("datemax", datemax);
		
		  int pageNum = (start/length)+1;
		  int pageSize = length;		 
		  
	      PageHelper.startPage(pageNum, pageSize);
	      List<Maintain> maintainList = maintainMapper.searchMaintain(params);
	      PageInfo<Maintain> page = new PageInfo<Maintain>(maintainList);
	      
	      Map<String,Object> rsMap = new HashMap<String,Object>();
	      rsMap.put("draw",draw);
	      rsMap.put("data", page.getList());	      
	      rsMap.put("recordsTotal", page.getTotal());
	      rsMap.put("recordsFiltered", page.getTotal());
	      return rsMap;
	}

}
